package gc3d.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Pierrick
 *
 */
public class GC3DServletPreview extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PrintWriter out = resp.getWriter();
		String png  = req.getParameter("png");
		String UUID = req.getParameter("UUID");
		if (png != null && UUID != null) {
			resp.setContentType("image/png");
			png = png.replace('\\', 's'); // secu
			png = png.replace('/' , 's'); // secu
			FileInputStream fis = new FileInputStream(
					System.getenv("FTP_HOME") 
					+ File.separatorChar + "GridPovTemp" 
					+ File.separatorChar + UUID 
					+ File.separatorChar + png);
			int buff;
			while ((buff = fis.read()) != -1) {
				out.write(buff);
			}
			fis.close();
		}
		else {
			resp.setContentType("text/html");
			out.print("<h1>?png=xxx.povXX.png&UUID=xxx<h1>");
		}
	}
}
